<?php

use Doraemon\service\Event\Es\EsRelatedEvent;
use Doraemon\tools\Log;

class EsOperateAction extends ActionPlugin
{
    const IS_TRACING = true;

    /**
     * @throws Exception
     */
    public function main()
    {
        Log::info('Es操作接收到的订阅消息', $this->messageBody);

        if (!$this->messageBody['database']) throw new \Exception('未获取到数据库');
        if (!$this->messageBody['table']) throw new \Exception('未获取到数据表');
        if (!$this->messageBody['data']) throw new \Exception('未接收到数据');

        $fieldsMap = EsRelatedEvent::matchFields($this->messageBody['database'], $this->messageBody['table']);
        Log::debug('field_map', [$fieldsMap]);
        if (!empty($fieldsMap)) EsRelatedEvent::operateEs($this->messageBody, $fieldsMap);

        $this->setSuccess();
    }
}